//
//  MusicPlayingViewController.swift
//  Reader
//
//  Created by mn on 2024/9/27.
//

import UIKit

extension UIColor{
    static func color(_ hex:UInt32, alpha:Float = 1) -> UIColor{
        let alphaValue = CGFloat(min(max(alpha, 0.0), 1.0))
        let red = CGFloat((hex & 0xFF0000) >> 16) / 255.0
        let green = CGFloat((hex & 0x00FF00) >> 8) / 255.0
        let blue = CGFloat(hex & 0x0000FF) / 255.0
        return UIColor(red: red, green: green, blue: blue, alpha: alphaValue)
    }
}

class MusicPlayingViewController: UIViewController {

    @IBOutlet weak var corverImageView: UIImageView!
    let musicList = MusicViewController()
    var file: File?{
        didSet{
            guard let image = file?.coverImage else { return }
            corverImageView.image = image
        }
    }
    lazy var gradientLayer: CAGradientLayer = {
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [UIColor.color(0x202020).cgColor,
                                UIColor.black.cgColor,
                                UIColor.color(0x202020).cgColor]
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 0.0, y: 1)
        return gradientLayer
    }()
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let animate = CABasicAnimation(keyPath: "transform.rotation.z")
        animate.toValue = Double.pi * 2
        animate.duration = 6
        animate.repeatCount = Float(CGFLOAT_MAX)
        corverImageView.layer.add(animate, forKey: "animate")
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        corverImageView.layer.removeAnimation(forKey: "animate")

    }
    @IBAction func tapAction(_ sender: UITapGestureRecognizer) {
        present(musicList, animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "音乐"
        corverImageView.layer.cornerRadius = CGRectGetWidth(corverImageView.frame)/2
        view.addSubview(MusicManagerView.shared)
        MusicManagerView.shared.changed = {
            [weak self] file in
            self?.file = file
        }
    }
    
}
